package com.example.service.impl;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.example.exception.ServiceException;
import com.example.mapper.UserMapper;
import com.example.pojo.entity.User;
import com.example.service.UserService;
import com.example.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Random;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    public void insert(User user){
        User dbuser = userMapper.selectByUsername(user.getUsername());
        if (dbuser != null){
            throw new ServiceException("用户名已存在");
        }
        if (StrUtil.isBlank(user.getName())){
            user.setName(user.getUsername());
        }
        if (StrUtil.isBlank(user.getPassword())){
            user.setPassword("123");
        }
        if (StrUtil.isBlank(user.getRole())){
            user.setRole("用户");
        }
        userMapper.insert(user);
    }

    @Override
    public void update(User user) {
        userMapper.update(user);
    }

    @Override
    public void delete(Integer id) {
        User currentUser = TokenUtils.getCurrentUser();
        if (currentUser != null && currentUser.getId() != null && id ==currentUser.getId() ){
            throw new ServiceException("不能删除自己");
        }
        if (currentUser.getRole().equals("管理员")){
            userMapper.delete(id);
        }else {
            throw new ServiceException("权限不足");
        }
    }

    @Override
    public void batchdelete(List<Integer> ids) {
        User currentUser = TokenUtils.getCurrentUser();
        if (currentUser.getRole().equals("管理员")){
            if (currentUser != null && currentUser.getId() != null && ids.contains(currentUser.getId())){
                throw new ServiceException("不能删除自己");
            }else {
                for (Integer id : ids){
                    userMapper.delete(id);
                }
            }
        }else {
            throw new ServiceException("权限不足");
        }
    }

    @Override
    public List<User> selectAll() {
        return userMapper.selectAll();
    }

    @Override
    public User selectById(Integer id) {
        return userMapper.selectById(id);
    }

    @Override
    public List<User> selectByName(String name) {
        return userMapper.selectByName(name);
    }

    @Override
    public List<User> selectMore(String name, String username) {
        return userMapper.selectMore(name,username);
    }

    @Override
    public List<User> selectByLike(String name, String username) {
        return userMapper.selectLike(name,username);
    }

    //用户登录
    @Override
    public User login(User user) {
        User dbuser = userMapper.selectByUsername(user.getUsername());
        if (dbuser == null ){
            throw new ServiceException("用户名或密码错误");
        }
        if (!user.getPassword().equals(dbuser.getPassword())){
            throw new ServiceException("用户名或密码错误");
        }
        //生成token
        String token = TokenUtils.createToken(dbuser.getId().toString(), dbuser.getPassword());
        dbuser.setToken(token);

        return dbuser;
    }

    @Override
    public User register(User user) {
        User dbuser = userMapper.selectByUsername(user.getUsername());
        if (dbuser != null){
            throw new ServiceException("用户名已存在");
        }
        user.setName("用户"+ RandomUtil.randomNumbers(4));
        userMapper.insert(user);
        return null;
    }

    @Override
    public void resetPassword(User user) {
        User dbuser = userMapper.selectByUsername(user.getUsername());
        if (dbuser == null){
            throw new ServiceException("用户名不存在");
        }
        if (!user.getPhone().equals(dbuser.getPhone())){
            throw new ServiceException("验证错误");
        };
        dbuser.setPassword("123");
        userMapper.update(dbuser);
    }

    @Override
    public PageInfo<User> selectByPage(Integer pageNum, Integer pageSize, String name, String username) {
        PageHelper.startPage(pageNum, pageSize);
        List<User> userList = userMapper.selectByPage(name, username);
        return new PageInfo<>(userList);
    }


}
